package com.itheima.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.itheima.common.R;
import com.itheima.domain.Employee;
import com.itheima.dto.LoginDto;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import com.itheima.vo.LoginVo;
import com.itheima.vo.Result;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * date: 2022/5/12 17:29
 *
 * @author daya000
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public LoginVo login(LoginDto dto) {
        LoginVo login = employeeMapper.login(dto);
        if(login==null){
            return null;
        }
        return login;
    }

    @Override
    public Employee loginRedis(LoginDto dto) {
        return employeeMapper.loginRedis(dto);
    }


    @Override
    public Boolean addEmployee(Employee employee) {
        long id = new Snowflake().nextId();
        employee.setId(Long.toString(id));

        //将页面提交的密码password进行md5加密
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        employee.setPassword(password);
        employee.setStatus(1);
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        employee.setCreateUser("11");
        employee.setUpdateUser("11");
        return employeeMapper.addEmployee(employee)>0;
    }

    @Override
    public Employee selectByUserName(String username) {
        return employeeMapper.selectByUsername(username);
    }

    @Override
    public Boolean updateEmployee(Employee employee) {
        if(Objects.isNull(employee)|| StrUtil.isEmpty(employee.getId())){
            return false;
        }
        employee.setUpdateTime(new Date());
        return employeeMapper.updateEmployee(employee)>0;
    }

    @Override
    public List<Employee> selectByPage(String name,Integer page, Integer pageSize) {
        Integer index=(page-1)*pageSize;
        return employeeMapper.selectByPage(name,index,pageSize);
    }

    @Override
    public Long selectTotal(String name) {
        return employeeMapper.selectTotal(name);
    }

    @Override
    public Employee selectById(String id) {
        if (Objects.isNull(id)){
            return null;
        }
        Employee employee = employeeMapper.selectById(id);
        return employee;

    }


}
